﻿package com.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.List;

import com.db.commute.CommuteVO;

public class DateProcess {

	private Calendar currentDay = Calendar.getInstance();
	private Calendar stDay = Calendar.getInstance();
	private Calendar edDay = Calendar.getInstance();	
	
	// 오늘 날짜의 년, 월 정보를 int의 배열로 반환한다
	public int[] getTodayCalendar(int year, int month){
		int[] result = new int[2];		
		stDay.set(year, month-1, 1);
		edDay.set(year, month-1, stDay.getActualMaximum(Calendar.DATE));
		result[0] = edDay.get(Calendar.DATE);
		result[1] = stDay.get(Calendar.DAY_OF_WEEK);
		return result;
	}
	
	// 년,월을 입력받아 마지막날을 구한다
	public int getLastDayOfMonth(int year, int month){
		int[] result = getTodayCalendar(year, month);
		return result[0];
	}
	
	// Date를 넘겨받아 마지막날을 구한다 (오버로딩) 
	public int getLastDayOfMonth(Date today){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String stoday = sdf.format(today);
		String sts[] = stoday.split("-");
		int year = Integer.parseInt(sts[0]);
		int month = Integer.parseInt(sts[1]);
		int[] result = getTodayCalendar(year, month);
		return result[0];
	}	
	
	//현재 년,월,일 구하기
	public int getCurrentYear(){
		int year = 0;
		year = currentDay.get(Calendar.YEAR);
		return year;
	}
	public int getCurrentMonth(){
		int month = 0;
		month = currentDay.get(Calendar.MONTH)+1;
		return month;
	}
	public int getCurrentDay(){
		int day = 0;
		day = currentDay.get(Calendar.DAY_OF_MONTH);
		return day;
	}
	
	// 오늘날짜를 Date로 입력받아 전일, 다음일을 구한다
	public Date[] getDays(Date today){
		Date[] days = new Date[3];
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(today);
		days[1] = tempCal.getTime();
		tempCal.add(Calendar.DATE, -1);
		days[0] = tempCal.getTime();
		tempCal.add(Calendar.DATE, +2);
		days[2] = tempCal.getTime();
		return days;
	}
	
	// 오늘 날짜를 입력받아 전주, 다음주의 WEEK_OF_YEAR를 구한다
	public Date[] getWeeks(Date today){
		Date[] days = new Date[3];
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(today);
		days[1] = tempCal.getTime();
		tempCal.add(Calendar.WEEK_OF_YEAR, -1);
		days[0] = tempCal.getTime();
		tempCal.add(Calendar.WEEK_OF_YEAR, +2);
		days[2] = tempCal.getTime();		
		return days;		
	}
	
	public int[] getWeekNums(Date today){
		int wnums[] = new int[3];
		Calendar tempCal = Calendar.getInstance();		
		tempCal.setTime(today);
		wnums[1] = tempCal.get(Calendar.WEEK_OF_YEAR);
		tempCal.add(Calendar.WEEK_OF_YEAR, -1);		
		wnums[0] = tempCal.get(Calendar.WEEK_OF_YEAR);
		tempCal.add(Calendar.WEEK_OF_YEAR, +2);		
		wnums[2] = tempCal.get(Calendar.WEEK_OF_YEAR);
		return wnums;		
	}
	
	public Date[] getOneWeek(Date today){
		Date[] wweek = new Date[3];
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(today);
		wweek[1] = tempCal.getTime();
		tempCal.set(Calendar.DAY_OF_WEEK, 1);
		wweek[0] = tempCal.getTime();
		tempCal.set(Calendar.DAY_OF_WEEK, 7);
		wweek[2] = tempCal.getTime();		
		return wweek;
	}
	
	// 오늘날짜의 WEEK_OF_YEAR를 int형으로 반환한다
	public int getTodayWeekNum(){
		Calendar tempCal = Calendar.getInstance();
		int weeknum = tempCal.get(Calendar.WEEK_OF_YEAR);
		return weeknum;
	}
	
	// 오늘날짜를 Date로 입력받아 전월, 다음월을 구한다
	public Date[] getMonths(Date today){
		Date[] days = new Date[3];
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(today);		
		days[1] = tempCal.getTime();
		tempCal.add(Calendar.MONTH, -1);
		days[0] = tempCal.getTime();
		tempCal.add(Calendar.MONTH, +2);
		days[2] = tempCal.getTime();
		return days;		
	}
	
	// int배열로 된 년,월,일,시,분 값을 받아 Date로 만들어 반환한다
	public Date valueToDate(int[] ymdhm){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, ymdhm[0]);
		cal.set(Calendar.MONTH, ymdhm[1]-1);
		cal.set(Calendar.DATE, ymdhm[2]);
		cal.set(Calendar.HOUR_OF_DAY, ymdhm[3]);
		cal.set(Calendar.MINUTE, ymdhm[4]);
		Date event = new Date(cal.getTimeInMillis());
		return event;
	}
	
	// int 값 년,월,일을 입력받아 Date로 변환 (오버로딩)
	public Date valueToDate(int year, int month, int day){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		cal.set(Calendar.DATE, day);
		Date result = new Date(cal.getTimeInMillis());
		return result;
	}
	
	// String으로된 날짜정보를 받아 Date로 변환 (오버로딩)
	public Date valueToDate(String sdate){
		// '-'로 나눈다 (2011-12-28 => {"2011", "12", "28"})
		String dtemp[] = sdate.split("-");		
		int year = Integer.parseInt(dtemp[0]);
		int month = Integer.parseInt(dtemp[1]);
		int day = 1;
		if(dtemp.length == 3){
			day = Integer.parseInt(dtemp[2]);			
		}
		return valueToDate(year, month, day);		
	}
	
	// Date값을 받아 형식을 포맷후 년,월,일을 별도로 배열에 담아 반환
	public String[] dateToValue(Date dt){		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String ymd = sdf.format(dt);
		String result[] = ymd.split("-");		
		return result;
	}
	
	// 해당 날짜의 정보만 있는 List를 받아 모든 날짜의 정보를 포함한 List로 변환
	// 고정된 달력 폼에 용이한 출력을 위함. 값이 없는 날짜엔 빈 vo를 포함
	public List<CommuteVO> convertList(int sabun, int endday, List<CommuteVO> list){
		List<CommuteVO> temp = new ArrayList<CommuteVO>();
		CommuteVO cvo = new CommuteVO();
		cvo.setSabun(sabun);		
		for(int i=0; i<endday; i++){
			temp.add(cvo);
		}		
		for(int i=0; i<list.size(); i++){
			cvo = list.get(i);
			SimpleDateFormat sdf = new SimpleDateFormat("dd");			
			String srd = sdf.format(cvo.getRegdate());
			int ind = Integer.parseInt(srd);			
			temp.set(ind-1, cvo);
		}
		return temp;
	}
	public List<CommuteVO> convertList(int sabun, Date startd, List<CommuteVO> list){
		SimpleDateFormat sdf = new SimpleDateFormat("dd");			
		List<CommuteVO> temp = new ArrayList<CommuteVO>();		
		CommuteVO cvo = new CommuteVO();
		Calendar cal = Calendar.getInstance();
		String strDay = sdf.format(startd);
		int stday = Integer.parseInt(strDay);
		cvo.setSabun(sabun);
		for(int i=0; i<7; i++){
			temp.add(cvo);
		}		
		for(int i=0; i<list.size(); i++){
			cvo = list.get(i);
			cal.setTime(cvo.getRegdate());			
			int ind = cal.get(Calendar.DAY_OF_WEEK);						
			//System.out.println(ind+"|"+i);
			temp.set(ind-1, cvo);
		}
		return temp;		
	}
}